package cn.jji8.createX.function.wand.canPlaceBlock;

import cn.jji8.createX.function.wand.Wand;
import org.bukkit.Effect;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerQuitEvent;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

public class WandFunctionState {
    static final Map<Player, WandFunctionState> map = new HashMap<>();

    /**
     * 玩家退出时取消掉
     * */
    public static void playerQuitEvent(PlayerQuitEvent e){
        WandFunctionState fillCanPlaceBlockState = map.remove(e.getPlayer());
        if(fillCanPlaceBlockState!=null){
            fillCanPlaceBlockState.setCancel(true);
        }
    }

    boolean cancel = false;
    //玩家
    Player player;
    //替换的方块
    Material replaceMaterial;
    //被替换的方块
    Material bookMaterial;
    //计数
    int count = 0;

    Wand wand;
    WandFunction wandFunction;
    public WandFunctionState(Player player, Material replaceMaterial,Material bookMaterial,Wand wand,WandFunction wandFunction) {
        map.put(player,this);
        this.player = player;
        this.replaceMaterial = replaceMaterial;
        this.bookMaterial = bookMaterial;
        this.wand = wand;
        this.wandFunction = wandFunction;
    }

    public boolean isCancel() {
        return cancel;
    }
    public void setCancel(boolean cancel) {
        this.cancel = cancel;
    }

    long lastTiem = 0;
    public void playEffectSTEP_SOUND(Block block) {
        if(System.currentTimeMillis()<lastTiem+40){
            return;
        }
        lastTiem = System.currentTimeMillis();
        Objects.requireNonNull(block.getWorld()).playEffect(block.getLocation(), Effect.STEP_SOUND,block.getType());
    }
}
